import { Controller, Get, Post, Body, Patch, Param, Delete ,Request,Headers, HttpCode,Res, Session, Inject} from '@nestjs/common';
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';

import * as svg from 'svg-captcha'//生成验证码

@Controller({
  path:'user',
  // 版本控制路径上加v1
  version:'1'
})
export class UserController {
  constructor(private readonly userService: UserService
  ) {}

  @Post('login')
  @HttpCode(200)
  create(@Body() createUserDto: CreateUserDto,@Request() req,@Headers() header) {
    console.log(req.body,req.session.code)
    return this.userService.create(createUserDto,req.body,req.session.code);
  }

  @Get()
  findAll(@Request() req,@Res() res,@Session() session) {
    console.log(req.session.code)
    const Cat =  svg.create({
      size:4,//生成几个验证码
      width:100,
      height:100,
      background:'#999999'
    })
    // return Cat.data;
    req.session.code = Cat.text;
    res.type('image/svg+xml')
    res.send(Cat.data)
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.userService.findOne(+id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.userService.remove(+id);
  }
}
